<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
    <!--样式-->
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .login-container {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        .login-container h2 {
            text-align: center;
            margin-bottom: 20px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
        }

        .form-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .login-btn {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }

        .login-btn:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
<div class="login-container">
    <h2>用户登录</h2>
    <form id="loginForm">
        <div class="form-group">
            <label for="username">账号:</label>
            <input type="text" id="username" name="username" required>
        </div>
        <div class="form-group">
            <label for="password">密码:</label>
            <input type="password" id="password" name="password" required>
        </div>
        <button type="submit" class="login-btn">登录</button>
    </form>
</div>
<!--引入jQuery-->
<script src="lib/js/jquery-3.7.1.min.js"></script>
<script>
$(document).ready(function() {
    /*
    监听表单提交事件
     */
    $('#loginForm').on('submit', function(e) {
        e.preventDefault();
        const username = $('#username').val();
        const password = $('#password').val();
        if (username.trim().lengt==0 || password.trim().length==0) {
            alert('请填写账号和密码');
            return;
        }
        // 使用jQuery的ajax发送登录请求
        $.ajax({
            url: '/user/login',
            type: 'POST',
            data: {
                username: username,
                password: password
            },
            dataType: 'json',//响应的数据格式要是json
            success: function(data) {
                if(data.code==200){
                    // 登录成功，跳转到index.html
                    //alert('登录成功');
                    window.location.href = 'index.html';
                }else {
                    alert(data.msg);
                }

            },
            error: function(xhr, textStatus, errorThrown) {
                // 登录失败，显示错误提示
                alert('登录失败，请检查账号和密码');
            }
        });
    });
});
</script>
</body>
</html>
